//
//  MainIconView.m
//  QuickCure
//
//  Created by Kent on 16/9/13.
//  Copyright © 2016年 haoge. All rights reserved.
//

#import "MainIconView.h"
#import "MainIconCell.h"
#import "BBZCommonWealController.h"
#import "DoctorInfoCollectionView.h"
#import "DoctorViewController.h"

static NSString *reuseCcell = @"collectionViewCell";

#define cHeight 90

@interface MainIconView () <UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,weak) UILabel *titleLabel;

@property (nonatomic,weak) UICollectionView *collectionView;

@property (nonatomic,strong) NSArray *picArray;

@property (nonatomic,strong) NSArray *nameArray;

@property (nonatomic,assign) NSInteger flag;

@end

@implementation MainIconView

-(NSArray *)picArray {
    if (!_picArray) {
        _picArray = @[[UIImage imageNamed:@"zl"],[UIImage imageNamed:@"xyg"],[UIImage imageNamed:@"sj"],[UIImage imageNamed:@"xy"],[UIImage imageNamed:@"gt"],[UIImage imageNamed:@"gy"]];
    }
    return _picArray;
}

-(NSArray *)nameArray {
    if (!_nameArray) {
        _nameArray = @[@"肿瘤",@"心血管",@"神经科",@"血液",@"骨科",@"公益"];
    }
    return _nameArray;
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
    
        [self generateContent];
        
    }
    return self;
}

- (void) generateContent {
  
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.text = @"名医通";
    titleLabel.font = FONT(12);
    titleLabel.textColor = kThemeTextGray;
    [self addSubview:titleLabel];
    self.titleLabel = titleLabel;
    
    [titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(42);
        make.top.equalTo(self).offset(4);
    }];
    
    [self generateCollectionView];
    
}


-(void) generateCollectionView {
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(sWidth/3, cHeight);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.delegate = self;
    collectionView.dataSource = self;
    
    [collectionView registerClass:[MainIconCell class] forCellWithReuseIdentifier:reuseCcell];
    
    self.collectionView = collectionView;
    
    [self addSubview:collectionView];
    [collectionView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.bottom);
        make.left.right.equalTo(self);
        make.height.equalTo(cHeight * 2);
    }];


}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.item < 5) {
        DoctorViewController *dvc = [[DoctorViewController alloc]init];
        dvc.title = self.nameArray[indexPath.item];
        if ([self.delegate respondsToSelector:@selector(jumpToVC:)]) {
            [self.delegate jumpToVC:dvc];
        }
    }
    if (indexPath.item == 5) {
        BBZCommonWealController *bbzcwc = [[BBZCommonWealController alloc]init];
        if ([self.delegate respondsToSelector:@selector(jumpToVC:)]) {
            [self.delegate jumpToVC:bbzcwc];
        }
    }
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.picArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    MainIconCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseCcell forIndexPath:indexPath];
    
    UIImage *img = self.picArray[indexPath.item];
    NSString *str = self.nameArray[indexPath.item];
    
    [cell UISetupWithName:str image:img];
    
    if (self.flag == 0) {
        NSIndexPath *ip = [NSIndexPath indexPathForItem:3 inSection:0];
        [collectionView scrollToItemAtIndexPath:ip atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
        self.flag ++;
    }
    
    return cell;
}












@end
























